home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 19.5 KB | 758 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: TextPart.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Anthone Burbidge
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef _TEXTPART_
- #include "TextPart.h"
- #endif
-
- #ifndef _TEXTFRAME_
- #include "TextFrame.h"
- #endif
-
- #ifndef _RULERFRAME_
- #include "RulerFrame.h"
- #endif
-
- #ifndef _TEXTUTIL_
- #include "TextUtil.h"
- #endif
-
- #ifndef _STYLEINFO_
- #include "StyleInfo.h"
- #endif
-
- #ifndef _TEXTPROXY_
- #include "TextProxy.h"
- #endif
-
- #ifndef _VALUESTREAM_
- #include "ValueStream.h"
- #endif
-
- #ifndef _TEXTSELC_
- #include "TextSelc.h"
- #endif
-
- #ifndef _ODTEXT_
- #include "ODText.h"
- #endif
-
- #ifndef _EMBEDRUN_
- #include "EmbedRun.h"
- #endif
-
- // ----- Textension Includes -----
-
- #ifndef _Textension_
- #include "Textension.h"
- #endif
-
- #ifndef _TSMTextension_
- #include "TSMTextension.h"
- #endif
-
- #ifndef _TextensionIOSuite_
- #include "TextensionIOSuite.h"
- #endif
-
- #ifndef _QDTextRun_
- #include "QDTextRun.h"
- #endif
-
- #ifndef _RulerObject_
- #include "RulerObject.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWPROXY_H
- #include "FWProxy.h"
- #endif
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _CMDDEFS_
- #include <CmdDefs.h>
- #endif
-
- #ifndef _STDPROPS_
- #include <StdProps.h>
- #endif
-
- #ifndef _MENUBAR_
- #include <MenuBar.h>
- #endif
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _DRAFT_
- #include <Draft.h>
- #endif
-
- #ifndef _WINDOW_
- #include <Window.h>
- #endif
-
- #ifndef _XMPSESSM_
- #include <XMPSessM.h>
- #endif
-
- #ifndef _DISPTCH_
- #include <Disptch.h>
- #endif
-
- #ifndef _SUCURSOR_
- #include <SUCursor.h>
- #endif
-
- #ifndef _INFO_
- #include <Info.h>
- #endif
-
- #ifndef _STDTYPES_
- #include <StdTypes.h>
- #endif
-
- #ifndef _TRANSLAM_
- #include <TranslaM.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #ifndef __FIXMATH__
- #include <FixMath.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef mathRoutinesIncludes
- #include <Math Routines.h>
- #endif
-
- // ----- TextPart Includes -----
-
- #pragma segment TextPartSegment
-
-
- //========================================================================================
- // CLASS CTextPart
- //========================================================================================
-
- const XMPPropertyName CTextPart::kTextPropertyName = "TextProperty";
- const XMPValueType CTextPart::kTextensionTextType = "TextensionTextType";
-
- CTextPart* CTextPart::gCurrentPart;
-
- //----------------------------------------------------------------------------------------
- // CTextPart::CTextPart
- //----------------------------------------------------------------------------------------
-
- CTextPart::CTextPart() :
- fFontMenu(NULL),
- fSizeMenu(NULL),
- fEmbedMenu(NULL),
- fEditText(NULL),
- fIsDrawing(false)
- {
- }
-
-
- //----------------------------------------------------------------------------------------
- // CTextPart::~CTextPart
- //----------------------------------------------------------------------------------------
-
- CTextPart::~CTextPart()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::Initialize
- //----------------------------------------------------------------------------------------
-
- void CTextPart::Initialize()
- {
- FW_CEmbeddingPart::Initialize();
-
- fFontMenu = ::NewMenu(kFontMenu,"\pFont");
- fSizeMenu = ::NewMenu(kSizeMenu,"\pSize");
- fEmbedMenu = ::NewMenu(kEmbedMenu,"\pEmbed");
-
- if (!fFontMenu || !fSizeMenu)
- ::DebugStr("\pError creating menus");
-
- ::AddResMenu(fFontMenu,'FOND');
-
- for (int i = 0; gFontSizeTable[i].fFontSize; i++)
- ::AppendMenu(fSizeMenu, gFontSizeTable[i].fFontSizeName);
-
- ::AppendMenu(fEmbedMenu,"\pText");
- ::AppendMenu(fEmbedMenu,"\pDraw");
- ::AppendMenu(fEmbedMenu,"\pClock");
- ::AppendMenu(fEmbedMenu,"\pMovie");
-
- XMPMenuBar *menuBar = GetMenuBar();
-
- menuBar->AddMenuLast(kFontMenu, fFontMenu, this);
- menuBar->AddMenuLast(kSizeMenu, fSizeMenu, this);
- menuBar->AddMenuLast(kSizeMenu, fEmbedMenu, this);
-
- // ----- Tokenize -----
- fRulerPresentation = GetSession()->Tokenize(CTextPart_kRulerPresentation);
- fMainPresentation = GetSession()->Tokenize(CTextPart_kMainPresentation);
- fScrollerPresentation = GetSession()->Tokenize(CTextPart_kScrollerPresentation);
-
- CreateEditText();
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::InternalizeContent
- //----------------------------------------------------------------------------------------
-
- void CTextPart::InternalizeContent(XMPStorageUnit* storageUnit)
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined,
- this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
-
- CValueStream *stream = new CValueStream;
- stream->InitValueStream(storageUnit);
-
- CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
- ioSuite->ITextensionIOSuite(false, stream);
-
- gCurrentPart = this;
-
- TReplaceParams replaceParams(ioSuite);
- OSErr err = fEditText->ReplaceRange(0/*start*/, 0/*end*/, &replaceParams, true/*invisible*/);
-
- gCurrentPart = NULL;
-
- TOffsetRange selRange((long) 0, (long) 0);
- if (!err)
- fEditText->SetSelectionRange(selRange);
-
- ioSuite->Free();
- delete ioSuite;
-
- stream->Free();
- delete stream;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::ExternalizeContent
- //----------------------------------------------------------------------------------------
-
- void CTextPart::ExternalizeContent(XMPStorageUnit* storageUnit)
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined,
- this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
-
- CValueStream *stream = new CValueStream;
- stream->InitValueStream(storageUnit);
-
- CTextensionIOSuite *ioSuite = new CTextensionIOSuite;
- ioSuite->ITextensionIOSuite(false, stream);
-
- gCurrentPart = this;
-
- TOffsetRange range2Save(0, fEditText->CountChars());
- OSErr err = fEditText->GetRangeIOSuite(&range2Save, ioSuite);
-
- gCurrentPart = NULL;
-
- // if (err)
- // ErrorAlert(err);
-
- ioSuite->Free();
- delete ioSuite;
-
- stream->Free();
- delete stream;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::Draw
- //----------------------------------------------------------------------------------------
-
- void CTextPart::Draw(XMPFacet* xmpFacet, XMPShape* invalidShape)
- {
- fIsDrawing = true;
- FW_CEmbeddingPart::Draw(xmpFacet, invalidShape);
- fIsDrawing = false;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::NewFrame
- //----------------------------------------------------------------------------------------
-
- FW_CFrame* CTextPart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
- {
- FW_CFrame* frame = NULL;
-
- if (presentation == fRulerPresentation)
- {
- frame = new CRulerFrame();
- ((CRulerFrame *) frame)->IRulerFrame(xmpFrame, this);
- }
- else
- {
- frame = new CTextFrame();
- ((CTextFrame *) frame)->ITextFrame(xmpFrame, this);
- }
-
- return frame;
- }
-
- //----------------------------------------------------------------------------------------
- // CDrawPart::NewSelection
- //----------------------------------------------------------------------------------------
-
- FW_CSelection* CTextPart::NewSelection()
- {
- CTextSelection* selection = new CTextSelection;
- selection->InitTextSelection(this);
- return selection;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::DoAdjustMenus
- //----------------------------------------------------------------------------------------
-
- void CTextPart::DoAdjustMenus(XMPMenuBar* menuBar)
- {
- FW_CEmbeddingPart::DoAdjustMenus(menuBar);
-
- short nItems, i;
- Str255 fontName, itemName;
- short fontSize;
-
- // ----- Check mark the font of the current selection.
-
- Boolean haveFontName = GetFont(fontName);
-
- nItems = ::CountMItems(fFontMenu);
- for (i = 1; i <= nItems; i++)
- {
- ::GetItem(fFontMenu, i, itemName);
- if (haveFontName && IUEqualString(itemName, fontName) == 0)
- ::CheckItem(fFontMenu, i, true);
- else
- ::CheckItem(fFontMenu, i, false);
- }
-
- // ----- Check mark the font size of the current selection.
-
- Str255 fontSizeName;
- Boolean haveFontSize = GetFontSize(fontSize);
- if (haveFontSize)
- ::NumToString(fontSize, fontSizeName);
-
- nItems = ::CountMItems(fSizeMenu);
- for (i = 1; i <= nItems; i++)
- {
- ::GetItem(fSizeMenu, i, itemName);
- if (haveFontSize && IUEqualString(itemName, fontSizeName) == 0)
- ::CheckItem(fSizeMenu, i, true);
- else
- ::CheckItem(fSizeMenu, i, false);
- }
-
- // ----- Edit Menu -----
- XMPTranslation *translate = GetSession()->GetTranslation();
- XMPType kTEXTType = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
- menuBar->EnableCommand(kXMPCommandSelectAll, fEditText->CountChars() > 0);
- menuBar->EnableCommand(kXMPCommandPaste,
- HasPropertyOnClipboard(kXMPPropContents, kSampleTextensionKind) ||
- HasPropertyOnClipboard(kXMPPropContents, kTEXTType));
- }
-
- //------------------------------------------------------------------------------
- // CTextPart::DoMenuEvent
- //------------------------------------------------------------------------------
-
- FW_Boolean CTextPart::DoMenuEvent(XMPMenuBar *menuBar, XMPCommandID commandID)
- {
- if (menuBar->IsCommandSynthetic(commandID))
- {
- XMPMenuID menu;
- XMPMenuItemID menuItem;
- menuBar->GetMenuAndItem(commandID, menu, menuItem);
- if (menu == kEmbedMenu)
- {
- HandleEmbedMenuEvent(menuItem);
- return TRUE;
- }
- }
-
- return FW_CEmbeddingPart::DoMenuEvent(menuBar, commandID);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetContentPropertyValueType
- //----------------------------------------------------------------------------------------
-
- XMPValueType CTextPart::GetContentPropertyValueType() const
- {
- return kSampleTextensionKind;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::NewProxyPart
- //----------------------------------------------------------------------------------------
-
- FW_CProxyRun* CTextPart::NewProxyRun()
- {
- CTextProxyRun *proxyRun = new CTextProxyRun();
- proxyRun->InitTextProxyRun(this);
- return proxyRun;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetDefaultPresentation
- //----------------------------------------------------------------------------------------
-
- XMPTypeToken CTextPart::GetDefaultPresentation() const
- {
- return fMainPresentation;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::SetFontSize
- //----------------------------------------------------------------------------------------
-
- void CTextPart::SetFontSize(short fontSize)
- {
- TOffsetRange range;
-
- long sizeFlag = 0;
- TAttrObjModifier modifier(kSizeAttr, &fontSize, sizeFlag);
-
- fEditText->GetSelectionRange(&range);
- fEditText->UpdateRangeRuns(&range, &modifier);
-
- InvalidateAllFrames(kContentFrameId, NULL, NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::SetFont
- //----------------------------------------------------------------------------------------
-
- void CTextPart::SetFont(const Str255 fontName)
- {
- TOffsetRange range;
- short fontNumber;
- long sizeFlag = 0;
-
- GetFNum(fontName, &fontNumber);
-
- TAttrObjModifier modifier(kFontAttr, &fontNumber);
-
- fEditText->GetSelectionRange(&range);
- fEditText->UpdateRangeRuns(&range, &modifier);
-
- InvalidateAllFrames(kContentFrameId, NULL, NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetFontSize
- //----------------------------------------------------------------------------------------
-
- Boolean CTextPart::GetFontSize(short& fontSize) const
- {
- TOffsetRange selRange;
- fEditText->GetSelectionRange(&selRange);
-
- const CRunObject* startRun = fEditText->Char2TextRun(selRange.Start());
- const CRunObject* endRun = fEditText->Char2TextRun(selRange.End());
-
- if (startRun == endRun && startRun != NULL)
- {
- startRun->GetAttributeValue(kSizeAttr, &fontSize);
- return true;
- }
- else
- return false;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetFont
- //----------------------------------------------------------------------------------------
-
- Boolean CTextPart::GetFont(Str255 fontName) const
- {
- TOffsetRange selRange;
- fEditText->GetSelectionRange(&selRange);
-
- const CRunObject* startRun = fEditText->Char2TextRun(selRange.Start());
- const CRunObject* endRun = fEditText->Char2TextRun(selRange.End());
-
- if (startRun == endRun && startRun != NULL)
- {
- short fontNumber;
- startRun->GetAttributeValue(kFontAttr, &fontNumber);
- ::GetFontName(fontNumber, fontName);
- return true;
- }
- else
- return false;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::SetSelectionJustification
- //----------------------------------------------------------------------------------------
-
- void CTextPart::SetSelectionJustification(CTextPart::Justification justify) const
- {
- CTextPart* self = (CTextPart *) this;
-
- TOffsetRange range;
- fEditText->GetSelectionRange(&range);
-
- TAttrObjModifier modifier(kJustAttr, &justify /*, kApplyJust */);
-
- fEditText->UpdateRangeRulers(&range, &modifier);
-
- self->InvalidateAllFrames(kContentFrameId, NULL, NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetSelectionJustification
- //----------------------------------------------------------------------------------------
-
- CTextPart::Justification CTextPart::GetSelectionJustification() const
- {
- TOffsetRange selRange;
- fEditText->GetSelectionRange(&selRange);
-
- const CRulerObject* currentRuler = fEditText->Offset2Ruler(selRange.rangeStart);
-
- Justification currJust;
- currentRuler->GetAttributeValue(kJustAttr, &currJust);
-
- return currJust;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::GetAnyFacet
- //----------------------------------------------------------------------------------------
-
- CTextFacet* CTextPart::GetAnyFacet(XMPTypeToken presentation)
- {
- FW_CFacet* facet = NULL;
- FW_CPartFrameIterator iter1(this, presentation);
- while (!iter1.IsDone())
- {
- FW_CFrameFacetIterator iter2(iter1.CurrentItem());
- if (!iter2.IsDone())
- {
- facet = iter2.CurrentItem();
- break;
- }
- iter1.Next();
- }
- return (CTextFacet *) facet;
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::HandleEmbedMenuEvent
- //----------------------------------------------------------------------------------------
-
- void CTextPart::HandleEmbedMenuEvent(short item)
- {
- XMPPart* embeddedPart;
-
- XMPStorageUnit* su = GetStorageUnit();
-
- switch (item)
- {
- case 1:
- embeddedPart = su->GetDraft()->CreatePart("Apple:Kind:Framework:Sample:Textension", NULL);
- break;
- case 2:
- embeddedPart = su->GetDraft()->CreatePart("Apple:Kind:Framework:Sample:QDraw", NULL);
- break;
- case 3:
- embeddedPart = su->GetDraft()->CreatePart("Apple:Kind:Framework:Sample:Clock", NULL);
- break;
- case 4:
- embeddedPart = su->GetDraft()->CreatePart("Apple:OSType:Scrap:moov", NULL);
- break;
- default:
- break;
- }
-
- // ----- Default frame shape -----
-
- FW_CRect frameRect(ff(0), ff(0), ff(80), ff(80));
-
- this->HandleEmbedPart(embeddedPart, frameRect);
-
- // ----- Release Part -----
- embeddedPart->Release();
-
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::HandleEmbedPart
- //----------------------------------------------------------------------------------------
-
- void CTextPart::HandleEmbedPart(XMPPart* xmpPart, const FW_CRect& frameRect)
- {
- XMPShape *frameShape = ::NewXMPShape(frameRect);
-
- // ----- Create a textension run object
-
- CEmbeddedRun* runObject = new CEmbeddedRun();
- runObject->InitEmbeddedRun();
-
- short selectionWidth = runObject->GetSelectionWidth();
-
- // ----- Get the current selection as the embedding position.
-
- TOffsetRange range;
- fEditText->GetSelectionRange(&range);
- FW_SPlatformPoint position = fEditText->Char2Point(range.Start());
- // ----- Account Textension border
- position.v += selectionWidth, position.h +=selectionWidth;
-
- XMPTransform* externalXForm = ::NewXMPTransform(position);
-
- // ----- Create the proxyPart -----
- CTextProxyRun* proxyRun = (CTextProxyRun *) NewProxyRun();
- FW_SPlatformRect qdFrameRect = frameRect;
- ::OffsetRect(&qdFrameRect, selectionWidth, selectionWidth);
- proxyRun->SetBoundingBox(qdFrameRect);
-
- // ----- Embed the part -----
- EmbedPart(xmpPart, proxyRun, fMainPresentation,
- GetSession()->Tokenize(kXMPViewAsFrame), kNoPresentation,
- externalXForm, frameShape, TRUE);
-
- // ----- ReplaceRange draws so we'd better be focused.
-
- CTextDrawInitiator di(GetAnyFacet(fMainPresentation));
-
- runObject->SetProxyRun(proxyRun);
-
- // Replace the current selection with the textension run object
-
- TReplaceParams replaceParams(runObject);
- fEditText->ReplaceRange(range.Start(), range.End(), &replaceParams);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::InitializeTextension
- //----------------------------------------------------------------------------------------
-
- void CTextPart::InitializeTextension()
- {
- static Boolean initialized = false;
-
- if (!initialized)
- {
- initialized = true;
-
- SetFractEnable(true);
-
- CTextension::TextensionStart();
-
- CQDTextRun* textRun = new CQDTextRun;
- textRun->IQDTextRun();
-
- CTextension::RegisterIORun(textRun);
-
- CEmbeddedRun *embeddedRun = new CEmbeddedRun;
- embeddedRun->InitEmbeddedRun();
-
- CTextension::RegisterIORun(embeddedRun);
-
- CBasicRuler* rulerObj = new CBasicRuler;
- rulerObj->IBasicRuler();
-
- CTextension::RegisterIORuler(rulerObj);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::CreateEditText
- //----------------------------------------------------------------------------------------
-
- void CTextPart::CreateEditText()
- {
- CTextPart::InitializeTextension();
-
- TTextensionHandlers textensionHandlers;
-
- fEditText = new COpenDocText;
- fEditText->InitOpenDocText(NULL, &textensionHandlers, kLargeSize);
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::StuffInTheText
- //----------------------------------------------------------------------------------------
-
- void CTextPart::StuffInTheText(FW_PlatformHandle text, Size textSize)
- {
- FW_UNUSED(text);
- FW_UNUSED(textSize);
- #if 0
- // TEInsert draws, but we're not ready to draw at the time the part is internalized,
- // because parts don't have grafports. So here we create a temporary port to draw
- // in and set the clip region to empty.
-
- GrafPtr savePort = (*fEditText)->inPort;
- GrafPort port;
- OpenPort(&port);
- SetRectRgn(port.clipRgn, 0, 0, 0, 0);
- (*fEditText)->inPort = &port;
-
- TEInsert((Ptr) *text, textSize, fEditText);
-
- ClosePort(&port);
- (*fEditText)->inPort = savePort;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // CTextPart::StuffInTheStyles
- //----------------------------------------------------------------------------------------
-
- void CTextPart::StuffInTheStyles(FW_PlatformHandle styles, Size stylesSize)
- {
- FW_UNUSED(styles);
- FW_UNUSED(stylesSize);
- #if 0
-
- SetStylScrap(0, 32767, (StScrpHandle) styles, false, fEditText);
- #endif
- }
-
-